package cn.hhhy.service.impl;

import cn.hhhy.mapper.ParkingMapper;
import cn.hhhy.pojo.Estate;
import cn.hhhy.pojo.PageResult;
import cn.hhhy.pojo.Parking;
import cn.hhhy.pojo.QueryPageBean;
import cn.hhhy.service.ParkingService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingServieImpl implements ParkingService {
    @Autowired
    private ParkingMapper parkingMapper;
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();

        IPage<Parking> page = parkingMapper.selectPage(new Page<>(currentPage,pageSize),
                Wrappers.lambdaQuery(Parking.class)
                        .like(!Strings.isEmpty(queryString), Parking::getUid, queryString)
                        .or()
                        .like(!Strings.isEmpty(queryString), Parking::getVid, queryString)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public Parking findById(Integer id) {
        return parkingMapper.selectById(id);
    }

    @Override
    public void add(Parking parking) {
        parkingMapper.insert(parking);
    }

    @Override
    public void delete(Integer id) {
        parkingMapper.deleteById(id);
    }

    @Override
    public void update(Parking parking) {
        System.out.println(parking.toString());
        parkingMapper.updateById(parking);
    }

    @Override
    public List<Parking> findByVID(Integer id) {
        return parkingMapper.selectList(Wrappers.lambdaQuery(Parking.class).eq(Parking::getVid,id));
    }

    @Override
    public List<Parking> findByUID(Integer id) {
        return parkingMapper.selectList(Wrappers.lambdaQuery(Parking.class).eq(Parking::getUid,id));
    }
}
